﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using MM.Common.Base;
using DevExpress.XtraEditors.Controls;
using DevExpress.Utils;
using System.Windows.Forms;
using DevExpress.XtraEditors.Repository;

namespace MM.Common.UI.Controls
{
    public class UCComboFromTable : UCComboData
    {
        // Fields
        private string _DefaultData;
        private string _ShowTab;
        private string _ShowTabOrder;
        private string _ShowTabWhere;
        private RepositoryItemLookUpEdit cbProperties;
        private IContainer components;

        // Methods
        public UCComboFromTable()
        {
            this._ShowTab = "";
            this._ShowTabOrder = "";
            this._ShowTabWhere = "";
            this._DefaultData = "";
            this.InitializeComponent();
        }

        public UCComboFromTable(IContainer container)
        {
            this._ShowTab = "";
            this._ShowTabOrder = "";
            this._ShowTabWhere = "";
            this._DefaultData = "";
            container.Add(this);
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public override void Init()
        {
            base.Init();
            if (!base.DesignMode)
            {
                StringBuilder builder = new StringBuilder("SELECT ");
                for (int i = 0; i < base.itemColumns.Count; i++)
                {
                    if (i > 0)
                    {
                        builder.Append(" ,");
                    }
                    builder.Append(base.itemColumns[i].FieldName.ToUpper());
                }
                builder.Append("\r\n    FROM ");
                builder.Append(this.ShowTab.ToUpper());
                if (this.Where != string.Empty)
                {
                    builder.Append("\r\n    WHERE ");
                    builder.Append(this.Where);
                }
                if (this.Order != string.Empty)
                {
                    builder.Append("\r\n    ORDER BY ");
                    builder.Append(this.Order);
                }
                DataTable dt = new DataTable();
                SystemInfo.DbData.Retrieve(builder.ToString(), ref dt);
                base.Properties.DataSource = dt;
            }
        }

        private void InitializeComponent()
        {
            this.cbProperties = new RepositoryItemLookUpEdit();
            this.cbProperties.BeginInit();
            base.SuspendLayout();
            this.cbProperties.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.cbProperties.CloseUpKey = new KeyShortcut(Keys.Shift | Keys.F4);
            this.cbProperties.Name = "cbProperties";
            this.cbProperties.NullText = "";
            base.ExistInputCode = true;
            this.cbProperties.EndInit();
            base.ResumeLayout(false);
        }

        // Properties
        [Description("本控件在新增时的缺省值"), DefaultValue(""), Category("MM附加"), Browsable(true)]
        public string DefaultData
        {
            get
            {
                return this._DefaultData;
            }
            set
            {
                this._DefaultData = value;
            }
        }

        [Browsable(true), DefaultValue(""), Description("数据表排序字段名称"), Category("MM附加")]
        public string Order
        {
            get
            {
                return this._ShowTabOrder;
            }
            set
            {
                this._ShowTabOrder = value;
            }
        }

        [Description("数据表表名"), DefaultValue(""), Browsable(true), Category("MM附加")]
        public string ShowTab
        {
            get
            {
                return this._ShowTab;
            }
            set
            {
                this._ShowTab = value;
            }
        }

        [DefaultValue(""), Category("MM附加"), Browsable(true), Description("数据表Where条件")]
        public string Where
        {
            get
            {
                return this._ShowTabWhere;
            }
            set
            {
                this._ShowTabWhere = value;
            }
        }
    }
}
